<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>语言列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">


    <link rel="stylesheet" href="../../../static/layui2/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../../../static/css/font_eolqem241z66flxr.css" media="all"/>
    <link rel="stylesheet" href="../../../static/css/user.css" media="all"/>
</head>
<body class="childrenBody">


<div style=" position: relative;">
    <blockquote class="layui-elem-quote news_search">
        <div class="layui-inline buttonTable">
            <div class="layui-input-inline">
                <input type="text"  name="name" value="" placeholder="请输入关键字" class="layui-input search_input">
            </div>
            <a class="layui-btn search_btn" data-type="reload"   >查询</a>
        </div>
        <div class="layui-inline buttonTable">
            <a class="layui-btn layui-btn-normal newsAdd_btn" data-type="newsAdd_btn"  >添加</a>
        </div>

        <div class="layui-inline buttonTable">
            <a class="layui-btn layui-btn-danger batchDel" data-type="batchDel"   >批量删除</a>
        </div>


    </blockquote>

    <table class="layui-table" id="dataTable" lay-filter="dataTable"></table>
    <script type="text/html" id="barDemo">
        {{#  if(d.pid== 0){ }}
        <a class="layui-btn layui-btn-xs"   lay-event="add_level">添加子项</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        {{#  } }}

        {{#  if(d.pid!= 0){ }}

        <a class="layui-btn layui-btn-xs" lay-event="edit_level">编辑</a>
        {{#  } }}

        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

</div>
<script type="text/javascript" src="../../../static/layui2/layui.js"></script>
<!--	<script type="text/javascript" src="demoList2.js"></script>-->

<script>
    var active={};
    layui.config({
        base: '../../../static/treetable-lay/module/'
    }).extend({
        treetable: 'treetable-lay/treetable'
    }).use(['jquery','table', 'form','layer', 'element', 'laydate', 'treetable'], function () {

        var table = layui.table,
            form = layui.form,
            laydate = layui.laydate,
            upload = layui.upload,
            treetable = layui.treetable,
            $ = layui.jquery;

         active = {
            //查询加载
            reload: function () {

                var name = $('input[name=name]').val();

                table.reload('dataTable', {
                    where: {
                        name: name
                    }
                });
            },
            renderTable:function(){
                renderTable();
            },
            //新增按钮
            newsAdd_btn: function () {
                var index = layui.layer.open({
                    title : "添加文章",
                    type : 2,
                    content : "add.html?type=add",
                    success : function(layero, index){
                        layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }
                })
                //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
                $(window).resize(function(){
                    layui.layer.full(index);
                })
                layui.layer.full(index);

            },
            //批量删除
            batchDel: function () {

                var ids="";

                var data = layui.table.checkStatus('dataTable').data;
                //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
                if(data.length==0){
                    layer.confirm('请选中要删除的数据', {offset: '150px'}, function (index) {
                        layer.close(index);
                    });
                   /* layer.msg("请选中要删除的数据", function () {
                    });*/
                }else{
                    data.forEach(function(item,index) {
                        var id=item.id;
                        if(index==0){
                            ids=id;
                        }else{
                            ids+=","+id;
                        }

                    });
                    layer.confirm('确认删除选中的数据吗', {offset: '150px'}, function (index) {
                        layer.close(index);
                        $.ajax({
                            url: "/ArticleController/dels",
                            data: {ids: ids},
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {

                                if (res.code === "00") {
                                    active.renderTable();
                                    form.render();
                                } else if (res.code === "01") {
                                    layer.msg(res.msg, function () {
                                    });
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    });


                }


            },

        };

        $('.buttonTable .layui-btn').on('click', function () {

            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        laydate.render({
            elem: '#dataDate'
        });

        var renderTable = function () {
            // 渲染表格
            treetable.render({
                treeColIndex: 2,          // treetable新增参数
                treeSpid: 0,             // treetable新增参数
                treeIdName: 'id',       // treetable新增参数
                treePidName: 'pid',     // treetable新增参数
                treeDefaultClose: true,   // 是否默认折叠
                treeLinkage: true,        // treetable新增参数
                elem: '#dataTable',
                url: '/ArticleController/getPageList',
                page: false,
                cols: [[
                    {type: 'numbers'}
                    , {type: 'checkbox'}
                    , {field: 'name', align: 'center', width: '35%', title: '名称'},

                    {field: 'desc', width: '35%', title: '描述'},
                    {fixed: 'right', width: '25%', align: 'center', title: '操作', toolbar: '#barDemo'}
                ]],
                done: function () {
                    layer.closeAll('loading');
                }

            });
        }
        active.renderTable();

        //监听工具
        table.on('tool(dataTable)', function (obj) {

            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确认删除此条数据吗', {offset: '150px'}, function (index) {
                    layer.close(index);
                    $.ajax({
                        url: "/ArticleController/del",
                        data: {id: data.id},
                        dataType: "json",
                        type: "post",
                        async: false,
                        success: function (res) {

                            if (res.code === "00") {
                                active.renderTable();
                                form.render();
                            } else if (res.code === "01") {
                                layer.msg(res.msg, function () {
                                });
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.error(XMLHttpRequest.status);
                            console.error(XMLHttpRequest.readyState);
                            console.error(textStatus);
                        }
                    });
                });
            } else if (obj.event === 'edit') {

                var index = layui.layer.open({
                    title : "修改",
                    type : 2,
                    content : "add.html?type=edit&id=" + data.id,
                    success : function(layero, index){
                        layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }
                })
                //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
                $(window).resize(function(){
                    layui.layer.full(index);
                })
                layui.layer.full(index);

            } else if (obj.event === 'add_level') {

                var index = layui.layer.open({
                    title : "新增子项",
                    type : 2,
                    content : "add_level.html?type=add&pid=" + data.id,
                    success : function(layero, index){
                        layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }
                })
                //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
                $(window).resize(function(){
                    layui.layer.full(index);
                })
                layui.layer.full(index);

            } else if (obj.event === 'edit_level') {

                var index = layui.layer.open({
                    title : "修改",
                    type : 2,
                    content : "add_level.html?type=edit&id=" + data.id+"&pid=" + data.pid,
                    success : function(layero, index){
                        layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }
                })
                //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
                $(window).resize(function(){
                    layui.layer.full(index);
                })
                layui.layer.full(index);

            }
        });


    });
</script>
</body>
</html>